package cn.jiyun.service;

import cn.hutool.system.UserInfo;
import cn.jiyun.mapper.UsersInfoMapper;
import cn.jiyun.pojo.UsersInfo;
import cn.jiyun.util.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService{
    @Autowired
    private UsersInfoMapper userInfoMapper;
    @Override
    public int register(UsersInfo usersInfo) {
        int i = userInfoMapper.insert(usersInfo);
        return i;
    }

    @Override
    public PageResult<UserInfo> queryList(Integer pageIndex, Integer pageSzie) {
        PageHelper.startPage(pageIndex, pageSzie);
        List<UserInfo> userInfos = userInfoMapper.selectAll();
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfos);
        PageResult<UserInfo> pageResult = new PageResult();
        pageResult.setLists(pageInfo.getList());
        pageResult.setPage(pageInfo.getPages());
        pageResult.setTotal((int) pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public Object queryUser(String username) {
        Example build = new Example.Builder(UsersInfo.class)
                .where(WeekendSqls.<UserInfo>custom()
                        .andEqualTo(userInfo -> userInfo.getName(), username))
                .build();


//        UserInfo users = userInfoMapper.selectOne(userInfo);
        UserInfo users = userInfoMapper.selectOneByExample(build);
        return users;    }

    @Override
    public boolean checkUserByPhone(String phone) {
        Example example = new Example(UserInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
//        UserInfo userInfo1 = new UserInfo();
//        userInfo1.setPhone(phone);
        UserInfo userInfo = userInfoMapper.selectOneByExample(example);
        return userInfo == null ? false : true;
    }
}
